//处理微信sdk的问题
import Vue from 'vue';
import {
	configJssdkUrl
} from '../../config';
import {
	post,
	showToast,
	getLocalStorage,
	deleteSessionTokenStr
} from './index';
//#ifdef H5
window.wx = require('weixin-js-sdk');
//#endif
//注入配置信息，每个页面都不一样
function config(url, showLoading = true) {
	return new Promise((resolve, reject) => {
		post(configJssdkUrl, {
				url: location.href,
				jsApiList: ['updateAppMessageShareData', 'updateTimelineShareData', 'startRecord', 'stopRecord',
					'onVoiceRecordEnd', 'previewImage',
					'playVoice', 'uploadVoice', 'downloadVoice', 'pauseVoice', 'onVoicePlayEnd', 'chooseWXPay',
					'onMenuShareAppMessage', 'onMenuShareTimeline'
				]
			}, showLoading)
			.then((data) => {
				wx.config(data);
				//data.link = url;
				let configObj = getLocalStorage('config');
				let obj = {
					title: configObj.share_title, // 分享标题
					link: deleteSessionTokenStr(url), // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
					imgUrl: configObj.share_picture, // 分享图标
					desc:configObj.share_desc,
					success: function() {
						//showToast('设置成功')
					},
					fail: function(res) {
						//showToast(JSON.stringify(res))
					},
				};
				
				shareData(obj);
				resolve(data);
			})
			.catch(() => {
				reject();
			});
	});
};


//分享接口
function shareData(obj) {
	wx.ready(() => {
		//wx.updateAppMessageShareData(obj); //分享给朋友或者QQ
		//wx.onMenuShareAppMessage(obj);
		//wx.updateTimelineShareData(obj);  //分享到朋友圈或者QQ空间
		//wx.onMenuShareTimeline(obj);
		console.log(wx.onMenuShareAppMessage);
		if (wx.onMenuShareAppMessage) {
			wx.onMenuShareAppMessage(obj); //1.0 分享到朋友
			wx.onMenuShareTimeline(obj); //1.0分享到朋友圈
		} else {
			wx.updateAppMessageShareData(obj); //1.4 分享到朋友
			wx.updateTimelineShareData(obj); //1.4分享到朋友圈
		}
	});
};


//隐藏分享接口
function hideShareMenu() {
	wx.ready(() => {
		wx.hideShareMenu({
			menuList: [
				'menuItem:share:appMessage', 'menuItem:share:timeline', 'menuItem:share:qq', 'menuItem:share:weiboApp',
				'menuItem:share:QZone'
			]
		})
	});
}


//调起微信支付
function wx_pay(obj) {
	//console.log(obj,111)
	return new Promise((resolve, reject) => {
		wx.ready(() => {
			wx.chooseWXPay({
				timestamp: obj.timestamp,
				nonceStr: obj.nonceStr,
				package: obj.package,
				signType: obj.signType,
				paySign: obj.paySign,
				success(res) {
					showToast('支付成功', 'success');
					resolve();
				},
				cancel(res) {
					reject();
				},
				fail(res) {
					showToast('支付失败,请重试');
					reject();
				}
			});
		});
	});
}

//开始录音
function wx_startRecord() {
	wx.ready(() => {
		wx.startRecord();
	});

}
// 停止录音接口
function wx_stopRecord() {
	return new Promise((resolve, reject) => {
		wx.ready(() => {
			wx.stopRecord({
				success: function(res) {
					var localId = res.localId;
					resolve(localId)
				}
			});
		});
	});
}
//监听录音自动停止
function wx_autostopRecord() {
	wx.ready(() => {
		wx.onVoiceRecordEnd({
			// 录音时间超过一分钟没有停止的时候会执行 complete 回调
			complete: function(res) {
				var localId = res.localId;
				resolve(localId)
			}
		});
	});
}
//播放语音接口
function wx_playRecord(content) {
	return new Promise((resolve, reject) => {
		wx.ready(() => {
			wx.playVoice({
				localId: content // 需要播放的音频的本地ID，由stopRecord接口获得
			});
		});
	});
}


module.exports = {
	config,
	shareData,
	wx_pay,
	hideShareMenu,
	wx_startRecord,
	wx_stopRecord,
	wx_autostopRecord,
	wx_playRecord
}
